#!/bin/bash

set -e
set -x

iplist_path=config
while read -r line
do
    case $line in
        *'#'*)
            continue
        ;;
        *'end'*)
            break
        ;;
        *)
            IFS=',' read -ra parts <<< "$line"
        ;;
    esac
    
    pass="${parts[0]}" 
    address="${parts[1]}"
    hostname="${parts[2]}"
    flag="${parts[3]}"
    
    if [ $address ];then
        if [ $flag != "local" ];then
            sshpass -p$pass ssh -n $address mkdir -p /root/.ssh
            sshpass -p$pass scp -r /root/.ssh/* $address:/root/.ssh/
        fi
        sshpass -p$pass ssh -n $address hostnamectl set-hostname $hostname
        sshpass -p$pass scp /etc/hosts $address:/etc/hosts
    fi
done < $iplist_path

set +x
set +e